﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using nwrole.plugin;

namespace nwrole.lib {
    public class StandardPluginProvider : PluginProviderBase {
        
        //private string serviceName;

        #region IPluginProvider

        public override void Initialize()
        {
            base.Initialize();

            CreatePluginConfigWatcher();
        }

        public override PropertyList LoadPluginSettings() {
            PropertyList plist = null;
            if (File.Exists(PluginConfigPath)) {
                plist = new PropertyList(PluginConfigPath);
            }
            return plist;
        }

        public override void SavePluginSettings(PropertyList plist) {

            plist.Save(PluginConfigPath);
        }

        /*public PropertyList LoadServiceSettings() {
            PropertyList plist = null;
            if (File.Exists(ServiceConfigPath)) {
                plist = new PropertyList(ServiceConfigPath);
            }
            return plist;
        }

        public void SaveServiceSettings(PropertyList plist) {
            plist.Save(ServiceConfigPath);
        }*/

        public override void RefreshPlugins() {
            var catalog = new AggregateCatalog();
            var container = new CompositionContainer(catalog);
            var batch = new CompositionBatch();
            batch.AddPart(this);
            catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
            catalog.Catalogs.Add(new DirectoryCatalog(ServicePath));
            container.Compose(batch);
        }

        #endregion

        private string ServicePath {
            get {
                string servicesDir = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "services");
                return Path.Combine(servicesDir, ServiceName);
            }
        }
        private string PluginConfigPath {
            get {
                return Path.Combine(ServicePath, "plugin-config.xml");
            }
        }
        
        private void CreatePluginConfigWatcher()
        {
            try
            {
                // Create a new FileSystemWatcher and set its properties.
                FileSystemWatcher watcher = new FileSystemWatcher();
                watcher.Path = Path.GetDirectoryName(PluginConfigPath);

                watcher.NotifyFilter = NotifyFilters.LastWrite;

                watcher.Filter = Path.GetFileName(PluginConfigPath);

                // Add event handlers.
                watcher.Changed += new FileSystemEventHandler(OnChanged);

                // Begin watching.
                watcher.EnableRaisingEvents = true;
            }
            catch(Exception e)
            {
                
            }
            
        }

        private void OnChanged(object source, FileSystemEventArgs e)
        {
            OnPluginSettingsChanged(new NwroleSettingsChangedEventArgs() { Name = "Plugin" });

            // Specify what is done when a file is changed, created, or deleted.
            //Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
        }
    }
}
